﻿''' <summary>
''' Empy page acting as a shell for all application content 
''' </summary>
''' <remarks>
''' Since this page has no content of it's own it simply relies
''' on all the default behaviour and settings from the base type
''' </remarks>
Partial Public Class Shell
	Inherits NavFx.HostPage

#Region "Overrides"
	Dim WithEvents myNavigator As NavFx.Navigator = MyBase.Navigator

	''' <summary>
	''' Override to load new pages into the panel provided
	''' </summary>
	''' <param name="newContent">New page to be loaded</param>
	Public Overrides Sub SetContent(ByVal newContent As System.Windows.Controls.UserControl)

		Me.contentPanel.Children.Clear()
		Me.contentPanel.Children.Add(newContent)

	End Sub

#End Region

#Region "Public members"

	''' <summary>
	''' Writes a line of text to the Output pane
	''' </summary>
	''' <param name="output">Output to be written</param>
	Friend Sub WriteOutput(ByVal output As String)

		Dim outputBlock As TextBlock = New TextBlock()
		outputBlock.Margin = New Thickness(2)
		outputBlock.FontSize = 12D
		outputBlock.TextAlignment = TextAlignment.Left
		outputBlock.Text = output
		Me.outputTextPanel.Children.Add(outputBlock)

		'TODO: get last block entered to scroll into view
		'if anyone knows how to do this let me know
	End Sub

#End Region

#Region "Private members"
	''' <summary>
	''' Helper method to populate the watch list of registered pages
	''' </summary>
	Private Sub loadRegisteredPages()

		'clear the list of registered pages
		'and re-populate, tried using binding
		'but got an exception, not concerned
		'about making it work with binding as
		'other than this demo can't see any real
		'scenarios where it would be needed
		Me.registeredPages.Items.Clear()
		For Each page As IPage In Me.Navigator
			Me.registeredPages.Items.Add(page.Path)
		Next

		'update the count of registered pages
		Me.pageCount.Text = Me.Navigator.PageCount.ToString()

		'write to output window
		Application.Current.WriteOutput(vbCrLf & "In loadRegisteredPages of Shell.xaml.vb executing...")
		Application.Current.WriteOutput("For Each page As IPage In Me.Navigator" & vbCrLf & vbTab & "Me.registeredPages.Items.Add(page.Path)" & vbCrLf & "Next")
		Application.Current.WriteOutput(vbCrLf & "Me.pageCount.Text = Me.Navigator.PageCount.ToString()")

	End Sub
#End Region

	Private Sub HostPage_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
		'register and load the home page
		'into the content panel
		'relying on the fact that a default
		'transition handler has been set by
		'the application
		Dim homePage As Home = New Home()
		Me.Navigator.RegisterPage(homePage)
		Me.Navigator.GoToPage("Home")

		'write to output window
		Application.Current.WriteOutput(vbCrLf & "In HostPage_Loaded of Shell.xaml.vb executing...")
		Application.Current.WriteOutput("Dim homePage As Home = New Home()")
		Application.Current.WriteOutput("Me.Navigator.RegisterPage(homePage)")
		Application.Current.WriteOutput("Me.Navigator.GoToPage(""Home"")")

		Me.loadRegisteredPages()


	End Sub


	'''<summary>
	''' Handles notification of page load completion from Navigator
	'''</summary>
	Private Sub Navigator_PageLoadCompleted(ByVal sender As Object, ByVal e As PageLoadCompletedEventArgs) Handles myNavigator.PageLoadCompleted
		Me.loadRegisteredPages()
	End Sub

	''' <summary>
	''' Loads the home page on click of the Home button
	''' </summary>
	Private Sub home_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles home.Click

		'Make sure the Transitor target is set to the application
		'and display the home page
		Me.Navigator.GetTransitor(Of SimpleTransitor)().Target = Application.Current
		Me.Navigator.GoToPage("Home")

		'write to output window
		Application.Current.WriteOutput(vbCrLf & "In home_Click of Shell.xaml.vb executing...")
		Application.Current.WriteOutput("Me.Navigator.GetTransitor(Of SimpleTransitor)().Target = Application.Current")
		Application.Current.WriteOutput("Me.Navigator.GoToPage(""Home"")")

		'update the list of registered pages
		'in the watch panel
		Me.loadRegisteredPages()
	End Sub

	''' <summary>
	''' Loads PanelPage1 from an external library on click of the External 1 button
	''' </summary>
	Private Sub external1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles external1.Click

		'Make sure the Transitor target is set to the application
		'and display the external page
		Me.Navigator.GetTransitor(Of SimpleTransitor)().Target = Application.Current
		Me.Navigator.GoToPage("PanelPage1", False, New Uri("NavFx.NavFxDemoVB.Pages.dll", UriKind.Relative))

		'write to output window
		Application.Current.WriteOutput(vbCrLf & "In external1_Click of Shell.xaml.vb executing...")
		Application.Current.WriteOutput("Me.Navigator.GetTransitor(Of SimpleTransitor)().Target = Application.Current")
		Application.Current.WriteOutput("Me.Navigator.GoToPage(""PanelPage1"", False, New Uri(""NavFx.NavFxDemoVB.Pages.dll"", UriKind.Relative))")

		'update the list of registered pages
		'in the watch panel in case this method is
		'called before external2_click
		Me.loadRegisteredPages()

	End Sub

	''' <summary>
	''' Loads PanelPage2 from an external library on click of the External 2 button
	''' </summary>
	Private Sub external2_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles external2.Click

		'Make sure the Transitor target is set to the application
		'and display the external page
		Me.Navigator.GetTransitor(Of SimpleTransitor)().Target = Application.Current
		Me.Navigator.GoToPage("PanelPage2", False, "NavFx.NavFxDemoVB.Pages.dll")

		'write to output window
		Application.Current.WriteOutput(vbCrLf & "In external2_Click of Shell.xaml.vb executing...")
		Application.Current.WriteOutput("Me.Navigator.GetTransitor(Of SimpleTransitor)().Target = Application.Current")
		Application.Current.WriteOutput("Me.Navigator.GoToPage(""PanelPage2"", False, ""NavFx.NavFxDemoVB.Pages.dll"")")

		'update the list of registered pages
		'in the watch panel in case this method is
		'called before external1_click
		Me.loadRegisteredPages()

	End Sub

	''' <summary>
	''' Displays IsPinned state of a Page on selection in registered pages list
	''' </summary>
	Private Sub registeredPages_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles registeredPages.SelectionChanged


		If Me.registeredPages.SelectedIndex <> -1 Then

			'the index based override of the indexer
			'is being used here to get a reference to
			'the page selected in registered pages
			Me.isPagePinned.IsChecked = Me.Navigator(Me.registeredPages.SelectedIndex).IsPinned

			'write to output window
			Application.Current.WriteOutput(vbCrLf & "In registeredPages_SelectionChanged of Shell.xaml.vb executing...")
			Application.Current.WriteOutput("Me.isPinned.IsChecked = Me.Navigator(Me.registeredPages.SelectedIndex).IsPinned")

		End If

	End Sub
End Class
